﻿@inherits BaseComponent

@if (Model != null && Model.QueryColumns != null && Model.QueryColumns.Count > 0)
{
    if (Model.QueryColumns.Count > 3)
    {
        var showCount = 3;
        var count = Model.QueryColumns.Count;
        if (count > showCount)
            count = _expand ? count : showCount;
        var offset = _expand ? (3 - count % 4) * 6 : 0;
        <div class="ant-query-more">
            <QueryDataForm @ref="form" Model="Model.QueryData">
                <AntRow>
                    @for (var i = 0; i < count; i++)
                    {
                        var item = Model.QueryColumns[i];
                        var itemName = Language.GetString(item);
                        <AntCol Span="6">
                            <QueryItem Label="@itemName" Item="item" Data="context" OnSearch="OnSearchAsync" />
                        </AntCol>
                    }
                    <AntCol Span="6" Offset="offset" Style="text-align:right;">
                        <div class="ant-form-item">
                            <Button Type="primary" Icon="search" OnClick="e=>OnSearchAsync()">@Language.Search</Button>
                            @if (IsAdvSearch)
                            {
                                <Button Type="primary" OnClick="e=>ShowAdvSearch()">@Language.AdvSearch</Button>
                            }
                            <Button Icon="undo" OnClick="()=>{form?.Reset();}">@Language.Reset</Button>
                            @if (Model.QueryColumns.Count > showCount)
                            {
                                <a style="margin-left:8px;font-size:12px;" @onclick="()=>{_expand=!_expand;}">
                                    <Icon Type="@(_expand ? "up" : "down")"></Icon> @(_expand ? Language["Collapse"] : Language["Expand"])
                                </a>
                            }
                        </div>
                    </AntCol>
                </AntRow>
            </QueryDataForm>
        </div>
    }
    else
    {
        <div class="ant-query">
            <QueryDataForm @ref="form" Model="Model.QueryData" Layout="@FormLayout.Inline">
                @for (var i = 0; i < Model.QueryColumns.Count; i++)
                {
                    var item = Model.QueryColumns[i];
                    var itemName = Language.GetString(item);
                    <QueryItem IsInline Label="@itemName" Item="item" Data="context" OnSearch="OnSearchAsync" />
                }
                <div class="ant-query-btn">
                    <Button Type="primary" Icon="search" OnClick="e=>OnSearchAsync()">@Language.Search</Button>
                    @if (IsAdvSearch)
                    {
                        <Button Type="primary" OnClick="e=>ShowAdvSearch()">@Language.AdvSearch</Button>
                    }
                    <Button Icon="undo" OnClick="()=>{form?.Reset();}">@Language.Reset</Button>
                </div>
            </QueryDataForm>
        </div>
    } 
}

@code {
    private bool _expand = false;
    private QueryDataForm form;
    private bool IsAdvSearch => AdvSearch || Model.AdvSearch;

    [Parameter] public TableModel Model { get; set; }
    [Parameter] public bool AdvSearch { get; set; }

    private async Task OnSearchAsync()
    {
        Model.Criteria.IsQuery = true;
        Model.Criteria.Query = Model.QueryData.Select(d => d.Value).ToList();
        await Model.RefreshAsync();
        Model.Criteria.IsQuery = false;
    }

    private void ShowAdvSearch() => Model.ShowAdvancedSearch(App);
}